package com.example.game.hff;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

//public class Sound {
//
//	// 소리를 낼 메소드, 파일 이름을 인자로 받는다.
//	public static void play(final String fileName, int loopCount) {
//		// 소리를 낼 때마다 하나의 쓰레드를 만든다.
//		// 이 쓰레드는 소리가 끝나면 소멸한다.
//		// new Thread(new Runnable() {
//		// public void run() {
//		try {
//				Clip clip = AudioSystem.getClip();
//				AudioInputStream aiStream = AudioSystem
//						.getAudioInputStream(new File(fileName));
//				clip.open(aiStream);
//				clip.loop(loopCount);
//				clip.start();
//			} catch (Exception ex) {}
//	}
//}

public class Sound {
    private static Clip clip;
    private static Clip introClip;
    private static Clip raceClip;
    private static Clip sonicClip;
    private static Clip hornClip;
    private static Clip brakeClip;
    private static Clip barkClip;
    private static Clip bikehornClip;
    private static Clip howlClip;
    private static Clip hohohahaClip;
    
    
    public static void load(){
    	try {
	    	File file = new File("race.wav");
	    	AudioInputStream raceSound = AudioSystem.getAudioInputStream(file);
	    	raceClip = AudioSystem.getClip();
	    	raceClip.open(raceSound);
	    	
	    	file = new File("Sonic.mid");
	    	AudioInputStream sonicSound = AudioSystem.getAudioInputStream(file);
	    	sonicClip = AudioSystem.getClip();
	    	sonicClip.open(sonicSound);
	    	
	    	file = new File("horn1.wav");
	    	AudioInputStream hornSound = AudioSystem.getAudioInputStream(file);
	    	hornClip = AudioSystem.getClip();
	    	hornClip.open(hornSound);
	    	
	    	file = new File("brake.wav");
	    	AudioInputStream brakeSound = AudioSystem.getAudioInputStream(file);
	    	brakeClip = AudioSystem.getClip();
	    	brakeClip.open(brakeSound);
	    	
	    	file = new File("bark.wav");
	    	AudioInputStream barkSound = AudioSystem.getAudioInputStream(file);
	    	barkClip = AudioSystem.getClip();
	    	barkClip.open(barkSound);
	    	
	    	file = new File("BikeHorn.wav");
	    	AudioInputStream bikehornSound = AudioSystem.getAudioInputStream(file);
	    	bikehornClip = AudioSystem.getClip();
	    	bikehornClip.open(bikehornSound);
	    	
	    	file = new File("howl.wav");
	    	AudioInputStream howlSound = AudioSystem.getAudioInputStream(file);
	    	howlClip = AudioSystem.getClip();
	    	howlClip.open(howlSound);
	    	
	    	file = new File("HOHOHAHA.wav");
	    	AudioInputStream hohohahaSound = AudioSystem.getAudioInputStream(file);
	    	hohohahaClip = AudioSystem.getClip();
	    	hohohahaClip.open(hohohahaSound);
	    	
	    	file = new File("Intro.wav");
	    	AudioInputStream introSound = AudioSystem.getAudioInputStream(file);
	    	introClip = AudioSystem.getClip();
	    	introClip.open(introSound);
	    	
    	}catch (Exception ex) {}
    }
    
    
    public static void Sound(String fileName) {
        // specify the sound to play
        // (assuming the sound can be played by the audio system)
        // from a wave File
        try {
            File file = new File(fileName);
            if (file.exists()) {
                AudioInputStream sound = AudioSystem.getAudioInputStream(file);
             // load the sound into memory (a Clip)
                clip = AudioSystem.getClip();
                clip.open(sound);
            }
            else {
                throw new RuntimeException("Sound: file not found: " + fileName);
            }
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Malformed URL: " + e);
        }
        catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Unsupported Audio File: " + e);
        }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Input/Output Error: " + e);
        }
        catch (LineUnavailableException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
        }


    // play, stop, loop the sound clip
    }
    public static void play(){
        clip.setFramePosition(0);  // Must always rewind!
        clip.start();
    }
    public static void play2(String fileName){
    	Clip clip = null;
    	if (fileName.equals("race")) {
    		clip = raceClip;    	  	
    	} else if (fileName.equals("Sonic")) {
    		clip = sonicClip;
    	} else if (fileName.equals("horn")) {
    		clip = hornClip;
    	} else if (fileName.equals("brake")) {
    		clip = brakeClip;
    	} else if (fileName.equals("bark")) {
    		clip = barkClip;
    	} else if (fileName.equals("bikehorn")) {
    		clip = bikehornClip;
    	} else if (fileName.equals("howl")) {
    		clip = howlClip;
    	} else if (fileName.equals("hohohaha")) {
    		clip = hohohahaClip;
    	} else if (fileName.equals("intro")) {
    		clip = introClip;
    	}
        clip.setFramePosition(0);  // Must always rewind!
        clip.start();
    }
    public static void loop(){
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    
    public static void loop2(){
        sonicClip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    public static void stop(){
    	if (sonicClip != null) 
            sonicClip.stop();    	
    }
    public static void stop2(){
    	if (introClip != null) 
            introClip.stop();    	
    }
}

